<template>
  <view class="company bg-#1f50f2">
    <CustomNavbar title="公司信息" auto-back bg-color="#1f50f2" left-icon-color="#fff" :title-style="{ color: '#fff' }" />
    <view class="min-h-100vh w-[750rpx] rounded-[40rpx_40rpx_0rpx_0rpx] bg-#ffffff px-24rpx py-40rpx">
      <view class="flex items-center">
        <view class="relative">
          <view class="mr-20rpx shrink-0 border-1rpx border-#d8d8d8 rounded-full border-solid">
            <up-image width="120rpx" height="120rpx" shape="circle" :src="companyInfo.logo" />
          </view>
          <view class="absolute bottom-[5rpx] right-[18rpx]">
            <up-icon name="checkmark-circle-fill" color="#1f50f2" size="36rpx" />
          </view>
        </view>
        <view>
          <view class="mb-12rpx text-[30rpx] text-[#1d2129] font-semibold leading-[42rpx]">
            {{companyInfo.name}}
          </view>
          <view class="mb-24rpx flex items-center text-left text-[24rpx] text-[#6e6d72] leading-[34rpx]">
            <view>{{companyInfo.province}}·{{companyInfo.city}}</view>
            <view class="mx-10rpx h-28rpx w-2rpx bg-#6e6d72" />
            <view>{{companyInfo.type}}</view>
            <view class="mx-10rpx h-28rpx w-2rpx bg-#6e6d72" />
            <view>成立{{companyInfo.born_year}}年</view>
            <view class="mx-10rpx h-28rpx w-2rpx bg-#6e6d72" />
            <view>{{companyInfo.listed}}</view>
          </view>
        </view>
      </view>
      <up-gap height="36rpx" />
      <up-line color="#ebebeb" />
      <up-gap height="32rpx" />
      <up-tabs :current="currentType" :list="list" line-color="transparent" :active-style="{
          color: '#1664FF',
          fontSize: '30rpx',
          transform: 'scale(1.05)',
        }" :inactive-style="{
          color: '#4e5968',
          fontSize: '26rpx',
          transform: 'scale(1)',
        }" item-style="padding:0 25rpx 5px; line-height: 36rpx;" @change="tabsChange" />
      <up-gap height="30rpx" />
      <view>
        <Info :info-detail="companyInfo" v-if="currentType === 0" :client-type="clientType" />
        <Job :job-list="jobList" v-if="currentType === 1" :client-type="clientType" />
        <Advantage @updateDetails="updateHeightDian" :company-inf="companyInfo" :info-detail="companyHighlights"
          v-if="currentType === 2" :client-type="clientType" />
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
  import Info from './components/info.vue';
  import Job from './components/job.vue';
  import Advantage from './components/advantage.vue';
  import { companyDetail, userJobList, companyHighlightsDetail } from '@/api/user';
  import { CompanyInfoUpdateParams, companyInfoUpdate } from '@/api/user/company';
  // import home06 from "@/static/images/tab/home-06.png";
  // import home10 from "@/static/images/tab/home-10.png";
  const home06 = 'https://yiliao.test03.qcw800.com/static/images/tab/home-06.png'
  const home10 = 'https://yiliao.test03.qcw800.com/static/images/tab/home-10.png'

  const currentType = ref(0);
  const list = ref<Array<TabItem>>([
    { name: '公司简介', type: 0 },
    { name: '在招职位', type: 1 },
    { name: '校招亮点', type: 2 },
  ]);
  const companyId = ref(0);
  interface TabItem {
    name : string;
    type : number;
  }
  const tabsChange = (tabItem : TabItem) => {
    currentType.value = tabItem.type;
  };
  const clientType = computed(() => {
    const { globalData } = getApp() || {};
    if (globalData) {
      return globalData.clientType;
    }
    return '';
  });

  const companyInfo = ref({
    id: 0,
    born_year: 0,
    logo: '',
    name: '',
    type: '',
    listed: '',
    province: '',
    city: '',
    adress: '',
    num: '',
    born_time: '',
    born_money: '',
    job_count: 0,
    job_num: 0,
    about_us: '',
    official_website: '',
    image: [],
    image_detail: []
  })
  async function getCompanyDetail(company_id : number = 0) {
    let params : any = {
      company_id: company_id
    }
    let r = await companyDetail(params);
    console.log('公司详情 res:::', r);
    let rr : any = {
      id: r.id,
      born_year: r.born_year,
      logo: r.url_logo,
      name: r.name,
      type: r.type == 1 ? "民营" : r.company.type == 2 ? "国企" : "合资",
      listed: r.listed == 1 ? "未上市" : "上市",
      province: r.province.name,
      city: r.city.name,
      adress: r.adress,
      num: r.num,
      born_time: r.born_time,
      born_money: r.born_money,
      job_count: r.job_count,
      job_num: r.job_num,
      about_us: r.about_us,
      official_website: r.official_website,
      image: r.image,
      image_detail: r.image_detail
    }
    companyInfo.value = rr;
  }

  const companyHighlights = ref({
    id: 0,
    highlights: [],
    subsidys: [],
    personnel_training: '',
    honor: [],
    seniors_say: '',
    seniors_say_img: '',
  })
  async function getCompanyHighlight(company_id : number = 0) {
    let params : any = {
      company_id: company_id
    }
    let r = await companyHighlightsDetail(params);
    console.log('公司亮点详情 res:::', r);
    let rr : any = {
      id: r.id,
      label_statutory_benefits: r.label_statutory_benefits || [],
      label_bonus: r.label_bonus || [],
      label_medical_care: r.label_medical_care || [],
      label_food: r.label_food || [],
      label_live: r.label_live || [],
      label_go: r.label_go || [],
      label_entertainment: r.label_entertainment || [],
      label_supplement: r.label_supplement || [],
      label_grow_up: r.label_grow_up || [],
      label_custom: r.label_custom || [],
      highlights: [],
      subsidys: [],
      subsidy_job: r.subsidy_job,
      subsidy_rent_house: r.subsidy_rent_house,
      subsidy_interview: r.subsidy_interview,
      subsidy_buy_house: r.subsidy_buy_house,
      subsidy_traffic: r.subsidy_traffic,
      subsidy_life: r.subsidy_life,
      subsidy_return_home: r.subsidy_return_home,
      subsidy_other: r.subsidy_other,
      personnel_training: r.personnel_training,
      honor: r.honor || [],
      seniors_say: r.seniors_say || '',
      seniors_say_img: r.seniors_say_img || ''
    }
    for (let i = 0; i < rr.label_statutory_benefits.length; i++) {
      rr.highlights.push(rr.label_statutory_benefits[i]);
    }
    for (let i = 0; i < rr.label_bonus.length; i++) {
      rr.highlights.push(rr.label_bonus[i]);
    }
    for (let i = 0; i < rr.label_medical_care.length; i++) {
      rr.highlights.push(rr.label_medical_care[i]);
    }
    for (let i = 0; i < rr.label_food.length; i++) {
      rr.highlights.push(rr.label_food[i]);
    }
    for (let i = 0; i < rr.label_live.length; i++) {
      rr.highlights.push(rr.label_live[i]);
    }
    for (let i = 0; i < rr.label_go.length; i++) {
      rr.highlights.push(rr.label_go[i]);
    }
    for (let i = 0; i < rr.label_entertainment.length; i++) {
      rr.highlights.push(rr.label_entertainment[i]);
    }
    for (let i = 0; i < rr.label_supplement.length; i++) {
      rr.highlights.push(rr.label_supplement[i]);
    }
    for (let i = 0; i < rr.label_grow_up.length; i++) {
      rr.highlights.push(rr.label_grow_up[i]);
    }
    for (let i = 0; i < rr.label_custom.length; i++) {
      rr.highlights.push(rr.label_custom[i]);
    }
    if (r.subsidy_job) {
      rr.subsidys.push({ label: '基层就业补贴', value: r.subsidy_job });
    }
    if (r.subsidy_rent_house) {
      rr.subsidys.push({ label: '住房补贴', value: r.subsidy_rent_house });
    }
    if (r.subsidy_interview) {
      rr.subsidys.push({ label: '面试补贴', value: r.subsidy_interview });
    }
    if (r.subsidy_buy_house) {
      rr.subsidys.push({ label: '人才购房补贴', value: r.subsidy_buy_house });
    }
    if (r.subsidy_traffic) {
      rr.subsidys.push({ label: '人才交通补贴', value: r.subsidy_traffic });
    }
    if (r.subsidy_life) {
      rr.subsidys.push({ label: '人才引进生活补贴', value: r.subsidy_life });
    }
    if (r.subsidy_return_home) {
      rr.subsidys.push({ label: '大学生返乡安家补贴', value: r.subsidy_return_home });
    }
    if (r.subsidy_other) {
      rr.subsidys.push({ label: '其他补贴', value: r.subsidy_other });
    }
    companyHighlights.value = rr;
  }

  const jobList : any = reactive([])
  async function getJobList(company_id : number) {
    let params = {
      company_id: company_id,
      status: 1
    };
    let result = await userJobList(params);
    console.log("公司职位列表 res list:::", result);
    jobList.splice(0, jobList.length);
    for (let i = 0; i < result.list.length; i++) {
      const r = result.list[i];
      let rr = {
        job_name: r.name,
        job_id: r.id,
        salary_min: r.salary_min,
        salary_max: r.salary_max,
        salary_count: r.salary_count,
        province: r.province.name,
        city: r.city.name,
        job_nature: r.nature,
        qualifications: "",
        highlights: r.highlights,
        company_id: r.company.id,
        company_logo: r.company.url_logo,
        company_name: r.company.name,
        company_type: r.company.type == 1 ? "民营" : r.company.type == 2 ? "国企" : "合资",
        company_listed: r.company.listed == 1 ? "未上市" : "上市",
        like: r.like,
        like_img: r.like == 1 ? home06 : home10,
        hr_avatar: r.user.avatar,
        hr_name: r.user.name,
        hr_duty: r.user.hr.duty.name,
        hr_id: r.user.id
      };
      let qualifications = "";
      for (let j = 0; j < r.qualifications.length; j++) {
        qualifications += r.qualifications[j] + " ";
      }
      rr.qualifications = qualifications;
      jobList.push(rr);
    }
  }

  onLoad((query : any) => {
    console.log('公司详情传递公司ID:::', query.company_id);
    companyId.value = query.company_id;
    getCompanyDetail(query.company_id);
    getJobList(query.company_id);
    getCompanyHighlight(query.company_id);
  });

  //亮点数据
  let highlightsList = reactive([])
  const updateHeightDian = (value) => {
    highlightsList = value.value
    onClickSubmitHighlights()
  }

  //更新公司信息接口-校招亮点
  const onClickSubmitHighlights = async () => {
    if (companyId.value == 0) {
      uni.showToast({ title: '公司异常' })
      return
    }
    const objToJsonStr = (params : String) => {
      let arrJson = []
      if (params === 'fdfl') {
        highlightsList.fdfl[0].value.map(v => {
          if (typeof v.value !== 'string') {
            v.value.map(item => {
              if (item.active) {
                arrJson.push(item.value)
              }
            })
          } else {
            if (v.active) {
              arrJson.push(v.value)
            }
          }
        })
      } else if (params === 'jj') {
        highlightsList.jj[0].value.map(v => {
          if (v.active) {
            arrJson.push(v.value)
          }
        })
      } else {
        highlightsList.shgh.map(v => {
          if (v.field === params) {
            v.value.map(vi => {
              if (vi.active) {
                arrJson.push(vi.value)
              }
            })
          }
        })
      }
      return JSON.stringify(arrJson)
    }
    /*
    `label_statutory_benefits` json '法定福利 多选json数组 例：[五险,一金...]',
    `label_bonus` json '奖金类 多选json数组 例：[年终奖,绩效奖...]',
    `label_medical_care` json DEFAULT NULL COMMENT '标签-医 多选json数组 例：[...]',
    `label_food` json DEFAULT NULL COMMENT '标签-食 多选json数组 例：[...]',
    `label_live` json DEFAULT NULL COMMENT '标签-住 多选json数组 例：[...]',
    `label_go` json DEFAULT NULL COMMENT '标签-行 多选json数组 例：[...]',
    `label_entertainment` json DEFAULT NULL COMMENT '标签-娱 多选json数组 例：[...]',
    `label_supplement` json DEFAULT NULL COMMENT '标签-补充 多选json数组 例：[...]',
    `label_grow_up` json DEFAULT NULL COMMENT '标签-学习/成长 多选json数组 例：[...]',
    `label_custom` json DEFAULT NULL COMMENT '标签-自定义 多选json数组 例：[...]',
    `subsidy_job` text '补贴-基层就业',
    `subsidy_rent_house` text '补贴-住房',
    `subsidy_interview` text '补贴-面试',
    `subsidy_buy_house` text '补贴-人才购房',
    `subsidy_traffic` text '补贴-交通',
    `subsidy_life` text '补贴-人才引进生活补贴',
    `subsidy_return_home` text '补贴-大学生返乡安家',
    `subsidy_other` text '补贴-其他',
    `personnel_training` text '人才培养',
    `honor` json DEFAULT NULL COMMENT '公司荣誉 json数组',
    `seniors_say` varchar(200) '师哥师姐说(视频地址)',
    */
    let params : CompanyInfoUpdateParams = {
      company_id: companyId.value,
      label_statutory_benefits: objToJsonStr('fdfl'),  // 法定福利 多选json数组 例：[五险,一金...]',
      label_bonus: objToJsonStr('jj'),  // 奖金类 多选json数组 例：[年终奖,绩效奖...]',
      label_medical_care: objToJsonStr('shgh_yi'),  // 标签-医 多选json数组 例：[...]',
      label_food: objToJsonStr('shgh_shi'),  // 标签-食 多选json数组 例：[...]',
      label_live: objToJsonStr('shgh_zhu'),  // 标签-住 多选json数组 例：[...]',
      label_go: objToJsonStr('shgh_xing'),  // 标签-行 多选json数组 例：[...]',
      label_entertainment: objToJsonStr('shgh_yu'),  // 标签-娱 多选json数组 例：[...]',
      label_supplement: objToJsonStr('shgh_buchong'),  // 标签-补充 多选json数组 例：[...]',
      label_grow_up: objToJsonStr('shgh_xuexi'),  // 标签-学习/成长 多选json数组 例：[...]',
      label_custom: objToJsonStr(["自定义标签1", "自定义标签2"]),  // 标签-自定义 多选json数组 例：[...]',
      subsidy_job: companyHighlights.value.subsidys[0].value,  // 补贴-基层就业
      subsidy_rent_house: companyHighlights.value.subsidys[1].value,  // 补贴-住房
      subsidy_interview: companyHighlights.value.subsidys[2].value,  // 补贴-面试
      subsidy_buy_house: companyHighlights.value.subsidys[3].value,  // 补贴-人才购房
      subsidy_traffic: companyHighlights.value.subsidys[4].value,  // 补贴-交通
      subsidy_life: companyHighlights.value.subsidys[5].value,  // 补贴-人才引进生活补贴
      subsidy_return_home: companyHighlights.value.subsidys[6].value,  // 补贴-大学生返乡安家
      subsidy_other: companyHighlights.value.subsidys[7].value,  // 补贴-其他
      // 人才培养
      personnel_training: "<p>1.参与公司SAAS平台产品的整体设计和产品规划。</p><p>2.参与公司SAAS平台和公司产品需求管理，完成需求分析和需求文档编写。</p><p>3.负责根据产品需求设计输出产品数据ER图。</p><p>4.负责独立设计产品原型，编写PRD等产品文档。</p><p>5.在整个研发周期中，密切配合研发规划排期并推进产品完成进度。</p>",
      honor: objToJsonStr([
        "https://pfsc.obs.cn-north-4.myhuaweicloud.com/images/594ac240dfb8b225f2f56be7be513306.png",
        "https://pfsc.obs.cn-north-4.myhuaweicloud.com/images/594ac240dfb8b225f2f56be7be513306.png2"
      ]),  // 公司荣誉 json数组
      seniors_say: "http://pfsc.obs.cn-north-4.myhuaweicloud.com/images/db380f44279a3e2c064923c875048f2d.jpeg",  // 师哥师姐说(视频地址)
    };
    console.log('参数：：：', params)
    let r = await companyInfoUpdate(params)
    if (r === 0) {
      console.log('用户信息更新成功')
      uni.showToast({
        title: '公司信息更新成功',
        icon: 'none'
      })
      uni.navigateTo(-1)
    } else {
      uni.showToast({
        title: '用户信息更新失败',
        icon: 'none'
      })
    }
  }

  //更新公司信息接口-公司简介
  const onClickSubmit = async () => {
    console.log('公司ID：：：', companyId.value)
    if (companyId.value == 0) {
      uni.showToast({ title: '公司异常' })
      return
    }
    const objToJsonStr = (param : Array<string>) => {
      return JSON.stringify(param)
    }
    /*
    `about_us` text '公司简介 富文本',
    `adress` text '公司地址',
    `official_website` varchar(200) '招聘官网',
    `image` json DEFAULT NULL COMMENT '企业介绍图片Json数组',
    `image_detail` json DEFAULT NULL COMMENT '企业风光Json图片数组',
    */
    let params : CompanyInfoUpdateParams = {
      company_id: companyId.value,  //公司ID
      //公司简介 富文本
      about_us: "<p>1.参与公司SAAS平台产品的整体设计和产品规划。</p><p>2.参与公司SAAS平台和公司产品需求管理，完成需求分析和需求文档编写。</p><p>3.负责根据产品需求设计输出产品数据ER图。</p><p>4.负责独立设计产品原型，编写PRD等产品文档。</p><p>5.在整个研发周期中，密切配合研发规划排期并推进产品完成进度。</p>",
      image: objToJsonStr([
        "https://pfsc.obs.cn-north-4.myhuaweicloud.com/images/594ac240dfb8b225f2f56be7be513306.png",
        "https://pfsc.obs.cn-north-4.myhuaweicloud.com/images/594ac240dfb8b225f2f56be7be513306.png2"
      ]),  // 企业介绍 json数组
      image_detail: objToJsonStr([
        "https://pfsc.obs.cn-north-4.myhuaweicloud.com/images/594ac240dfb8b225f2f56be7be513306.png",
        "https://pfsc.obs.cn-north-4.myhuaweicloud.com/images/594ac240dfb8b225f2f56be7be513306.png2"
      ]),  //企业风光 json数组
    };
    console.log('参数：：：', params)
    let r = await companyInfoUpdate(params)
    if (r > 0) {
      console.log('公司简介更新成功')
      uni.showToast({ title: '公司简介更新成功' })
    } else {
      uni.showToast({ title: '公司简介更新失败' })
    }
  }
</script>

<script lang="ts">
  export default {
    options: {
      styleIsolation: 'shared',
    },
  };
</script>

<style scoped lang="scss">
  .company {
    ::v-deep(.u-tabs) {
      .u-tabs__wrapper__nav__line {
        &::before {
          @apply content-[""] absolute left-0 top-0 w-[120rpx] h-[8rpx] opacity-50 translate-x-[-20px] rounded-full;

          background: linear-gradient(90deg,
              #1f69fb 3%,
              rgb(83 178 252 / 0%) 98%);
        }
      }
    }
  }
</style>
